<?php
/**
 * @name /www/unit-tests/TripItem.php
 * @author Kimberly Berger <knb4850@esu.edu>
 * @version  2011.03.30
 *
 * Unit test for User class.
 */

// Sets up configuration settings and includes libraries.
require_once('_ini.php');
// Libraries
ReqLib('/lib/private/TripItem.php');

function DefineHeader() {
?><style type="text/css">
  code {
    font: bold 10pt 'Courier New';
    color: #666;
    background-color: #CCC;
    border: 3px solid #666;
    padding: 4px 6px;
    margin: 10px 20px;
    display: block;
  }
  form, ul, ol {
    margin: 0px 20px;
  }
  a#Back {
    float: right;
    margin: 10px;
    padding: 4px 6px;
    border: 1px solid #666;
    background-color: #CCC;
  }
</style><?
}

/**
 * Defines page content.
 *
 */
function DefineContent() {
/* Methods:

	TripItem()

  "GET":
  TripItems[] GetTripItems(int offset, int maxResults)
  bool Exists(TripItemId)
  string ToString()

  "SCUD":
  bool Load(int TripItemID)
  bool Create(TripId, ItemID)
  bool Save()
  static bool Delete(TripItemId)

  Custom:
	GetTripItemIdByItemId($ItemId)
	GetTripItemIdbyTripId($TripId)
	GetTripItemIdbyTripAndItemId($TripId, $ItemId)
*/
  $actions = array(
    'Delete(id)',
    'Delete All',
    'Create',
    'View Details',
    'List(offset,max)',
    'GetIdsByItemID(itemid)',
    'GetIdsByTripId(tripid)',
	'GetIdsByTripIdAndItemId(tripid, itemid)',
    'Save'
  );
  // Page content goes here.
  ?>
<h1><a href="TripItem.php">TripItem - Unit Test</a></h1>
<a id="Back" href="index.php">Back to Unit Testing Home</a>
<form method="GET">
  <label for="Param1">Param 1: </label><input type="text" value="<?=htmlspecialchars($_GET['Param1']) ?>" name="Param1" /><br />
  <label for="Param2">Param 2: </label><input type="text" value="<?=htmlspecialchars($_GET['Param2']) ?>" name="Param2" /><br />
<?
  foreach ($actions as $action)
    println('<input type="submit" name="Action" value="' . $action . '" /><br />');
?><br />
<hr />
<?
  DoAction();
?></form>
<?
}

function DoAction() {
?><p>
<?
  switch ($_GET['Action']) {
    case 'Delete(id)':
      println(TripItem::Delete($_GET['Param1']) ? 'Deleted' : 'Not deleted');
      break;

    case 'Delete All':
      $tis = TripItem::GetTripItems();
      foreach ($tis as $ti)
        TripItem::Delete($ti->TripItemId);
      println('Data cleared.');
      break;

    case 'Create':
      $ti = new TripItem();
      $tis = array(
        array('', ''),
        array(rand(1,10), rand(1,10)),
        array(rand(1,10), rand(1,10)),
        array(rand(1,10), rand(1,10)),
        array(rand(1,10), rand(1,10)),
        array(rand(1,10), rand(1,10))
      );
      foreach ($tis as $data)
        println("Creating tripitem (tripid:'{$data[0]}'/itemid:'{$data[1]}'): " . ($ti->Create($data[0], $data[1]) ? 'Created' : 'Not created') . '<br />');
      break;

    case 'List(offset,max)':
	  println('Displays the TripItemId and the corresponding ItemName');
      println('<ol>');
      foreach(TripItem::GetTripItems($_GET['Param1'], $_GET['Param2']) as $ti)
        println("<li>{$ti->TripItemId}: {$ti->Name}</li>");
      println('</ol>');
      break;

    case 'View Details':
      println('<textarea style="width: 600px; height: 400px;">' . print_r(TripItem::GetTripItems(2, 1), true) . '</textarea>');
      break;

    case 'GetIdsByItemID(itemid)':
	 println('<textarea style="width: 600px; height: 400px;">'. "ByItemId ('" . htmlspecialchars($_GET['Param1']) . "') = '" . print_r((TripItem::GetTripItemIdsByItemId($_GET['Param1'])), true) . "' " .  '</textarea>');
     break;

    case 'GetIdsByTripId(tripid)':
      println('<textarea style="width: 600px; height: 400px;">'. "ByTripId ('" . htmlspecialchars($_GET['Param1']) . "') = '" . print_r((TripItem::GetTripItemIdsbyTripId($_GET['Param1'])), true) . "' " .  '</textarea>');
      break;
    case 'GetIdsByTripIdAndItemId(tripid, itemid)':
	println('<textarea style="width: 600px; height: 400px;">'. "ByTrip&ItemIds ('" . htmlspecialchars($_GET['Param1']) . "', '" . htmlspecialchars($_GET['Param2']) . "') = '" . print_r((TripItem::GetTripItemIdbyTripAndItemId($_GET['Param1'], $_GET['Param2'])), true) . "' " .  '</textarea>');
	  break;
    case 'Save':
      $ti = TripItem::GetTripItems(0, 1);
      if (!count($ti)) {
        $ti = new TripItem();
        $ti->Create(rand(1,10), rand(1,10));
      }
      else
      $ti = $ti[0];
      $ti->TripId = rand(1, 10);
      $ti->ItemId = rand(1,10);
      $ti->TripQuantity = rand(1, 100);
      $ti->DateTransfered = date('Y-m-d', mktime(0,0,0, rand(1,12), rand(1,28),rand(1000, 5000)));
      if($ti->Save()){
		println('Saved');
        println('<br /><textarea style="width: 600px; height: 400px;">tripitemid: ' . $ti->TripItemId . "\r\n" . print_r($ti->ToString("\r\n"), true) . '</textarea>');
		}
	  else
		 println('Not Saved');
      break;

    default:
      break;
  }
?></p>
<?
}

// Launches the page; without this, nothing happens.
require_once('_loader.php');